

window.appointment = {};
var detail = {
    getData: function() {
        $.ajax({
            url: window.domain + window.requestUrl.getDetailUrl,
            type: 'get',
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            data: {
                id: window.getUrlParam('id'),
                platId: 4
            },
            success: function(res) {
                detail.getrelatePlats();
                if(typeof(res.not_login) != 'undefined') {
                    window.setCookie('go_back_link', window.location.href);
                    window.location.href = './login.html';
                    return;
                }
                if(res.status) {
                    var result = res.data.result;
                    $('[data-role="name"]').html(result.title);
                    $('[data-role="registerTime"]').html('注册时间：' + result.createTime);
                    var fansReal = result.fansCount ? result.fansCount : '无';
                    $('[data-role="fans"]').html('粉丝数：' + fansReal);
                    $('[data-role="updateTime"]').html( '更新时间：' + result.updateTime);
                    var avgReadCountReal =  result.avgReadCount ? result.avgReadCount : '无';
                    $('[data-role="readCount"]').html('参考阅读量：' + avgReadCountReal);
                    var addressTpl = '';
                    var positionMap = JSON.parse(result.position);
                    if(positionMap.city != 'null' && positionMap.city != 'city') {
                        addressTpl += '-' + positionMap.city;
                    }
                    if(positionMap.county != 'null' && positionMap.county != 'county') {
                        addressTpl += '-' + positionMap.county;
                    }
                    $('[data-role="address"]').html('所在地址：' + positionMap.province + addressTpl);
                    $('[data-role="intro"]').html('简介：' + (result.description ? result.description : '无'));
                    $('.detail-info img').prop('src', result.iconUrl);
                    $('.detail-info__fixed img').prop('src', result.url ? JSON.parse(result.url) : '');

                    $('[data-role="ggzf"]').append(result.price1 == -1 ? '无' : result.price1);
                    $('[data-role="ggzr"]').append(result.price2 == -1 ? '无' : result.price2);

                    var qqTpl = '<a href="tencent://message/?uin='+result.qq+'&Site=yoomeda.com&Menu=yes" target="_blank">\
                                    <img src="http://wpa.qq.com/pa?p=2:'+result.qq+':41" alt="点击这里给我发消息" title="点击这里给我发消息">\
                                </a>';
                    $('.detail-top__qq').html(qqTpl);

                    // 标签
                    var tagsTpl = '';
                    $.each(JSON.parse(res.data.result.tags), function(index, item) {
                        tagsTpl += item + ' ';
                    })
                    $('[data-role="tags"]').html(tagsTpl);

                    var cases = res.data.case;
                    var successDemoTpl = '';
                    if(cases.length == 0) {
                        if(window.isLogin) {
                            $('[data-role="successDemo"] .detail-partRight__none').show().find('a').attr('href', 'tencent://message/?uin='+result.qq+'&Site=yoomeda.com&Menu=yes');
                        } else {
                            $('[data-role="successDemo"] .detail-partRight__none').show().find('a').attr('href', 'javascript:;').attr('data-role', 'tologin');
                        }

                    } else {
                        $.each(cases, function(index, item) {
                            successDemoTpl += '<li>\
                                                <p class="name">案例事件：'+item.name+'</p>\
                                                <p>推广时间：'+window.formatTime(item.opTime)+'</p>\
                                                <p>合作客户：'+item.customer+'</p>\
                                                <p>推广效果：'+item.effect+'</p>\
                                                <p>合作链接：<a href="'+item.link+'" target="_blank">'+item.link+'</a></p>\
                                            </li>'
                        })
                        $('[data-role="successDemo"]').html(successDemoTpl);
                    }

                    // 公司
                    $('.detail-company .name').html('工作室：' + result.companyName);
                    $('.detail-company a').attr('href', './company-show.html?id='+result.ownerId);

                    ////////////
                    //联系小编
                    $('.detail-topRight img').attr('src', result.companyIcon);
                    $('.detail-topRight p').html(result.companyName);
                    if(window.isLogin) {
                        $('.detail-topRight a').attr('href', 'tencent://message/?uin='+result.qq+'&Site=yoomeda.com&Menu=yes');
                    } else {
                        $('.detail-topRight a').attr('href', 'javascript:;').attr('data-role', 'tologin');
                    }


                    if(result.introduction) {
                        $('[data-role="mediaIntro"]').html(result.introduction);
                    } else {
                        if(window.isLogin) {
                            $('[data-role="mediaIntro"] .detail-partRight__none').show().find('a').attr('href', 'tencent://message/?uin='+result.qq+'&Site=yoomeda.com&Menu=yes');
                        } else {
                            $('[data-role="mediaIntro"] .detail-partRight__none').show().find('a').attr('href', 'javascript:;').attr('data-role', 'tologin');
                        }

                    }

                    if(result.cooperationTips) {
                        $('[data-role="co-intro"]').html(result.cooperationTips);
                    } else {
                        if(window.isLogin) {
                            $('[data-role="co-intro"] .detail-partRight__none').show().find('a').attr('href', 'tencent://message/?uin='+result.qq+'&Site=yoomeda.com&Menu=yes');
                        } else {
                            $('[data-role="co-intro"] .detail-partRight__none').show().find('a').attr('href', 'javascript:;').attr('data-role', 'tologin');
                        }

                    }

                    // 运营数据
                    var platParams = res.data.platParams;
                    $('[data-role="fansdata"]').html(result.fansCount ? result.fansCount: '无');
                    $('[data-role="avgReadCount"]').html(platParams.avgReadCount ? platParams.avgReadCount : '无');

                    var schedules = res.data.sched;
                    $.each(schedules, function(index, item) {
                        window.appointment[item.dt] = 1;
                    })
                    $('.calendar-nextmonth').trigger('click');
                    $('.calendar-prevmonth').trigger('click');
                } else {
                    iosOverlay({
                        text: res.msg,
                        duration: 1000
                    });
                }
            },
            error: function(e) {
            }
        })
    },
    getrelatedPages: function() {
        $.ajax({
            url: window.domain + window.requestUrl.relatedPagesUrl,
            type: 'get',
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            data: {
                id: window.getUrlParam('id'),
                platId: 4,
                pageSize: 10,
                pageNum: 1
            },
            success: function(res) {
                if(typeof(res.not_login) != 'undefined') {
                    window.setCookie('go_back_link', window.location.href);
                    window.location.href = './login.html';
                    return;
                }
                if(res.status) {
                    // 历史文章
                    var hisTpl = '';
                    if(res.data.result.list.length) {
                        $.each(res.data.result.list, function(index, item) {
                            var hrefTpl = '';
                            if(item.url) {
                                hrefTpl = '<p class="name"><a href="'+item.url+'" target="_blank">'+item.title+'</a></p>';
                            } else {
                                hrefTpl = '<p class="name">'+item.title+'</p>';
                            }
                            var readCount = item.readCount == null ?  '无' : item.readCount;
                            hisTpl += '<li>'+hrefTpl+'<div class="controls">\
                                            <p class="controls-time">'+item.publishTime+'</p>\
                                            <p class="controls-read"><span></span>'+readCount+'</p>\
                                        </div>\
                                    </li>';
                        })
                        //<span class="controls-zan">'+item.likeCount+'</span>
                        $('[data-role="history-article"]').html(hisTpl);
                    } else {
                        $('[data-role="history-article"] .detail-partRight__none').show();
                    }
                } else {
                    iosOverlay({
                        text: res.msg,
                        duration: 1000
                    });
                }
            },
            error: function(e) {
            }
        })
    },
    getrelatePlats: function() {
        $.ajax({
            url: window.domain + window.requestUrl.relatePlatsUrl,
            type: 'get',
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            data: {
                id: window.getUrlParam('id'),
                platId: 4
            },
            success: function(res) {
                if(typeof(res.not_login) != 'undefined') {
                    window.setCookie('go_back_link', window.location.href);
                    window.location.href = './login.html';
                    return;
                }
                if(res.status) {
                    $('[data-role="meitinum"]').append(res.data.result.length + 1);
                } else {
                    iosOverlay({
                        text: res.msg,
                        duration: 1000
                    });
                }
            },
            error: function(e) {
            }
        })
    },
    schedule: function() {
        $('body').append('<script src="assets/jquery.easyui.min.js"></script><script src="assets/jquery.fullcalendar.js"></script>')

    },
    loginStatus: function() {
        $.ajax({
            url: window.domain + window.requestUrl.userUrl,
            type: 'get',
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function(res) {
                if(typeof(res.not_login) != 'undefined') {
                    window.setCookie('go_back_link', window.location.href);
                    window.location.href = './login.html';
                    return;
                }
                detail.getData();
                if(res.status) {
                    window.isLogin = true;
                } else {
                    window.isLogin = false;
                }
            },
            error: function(e) {
            }
        })
    },
    tologin: function() {
        $(document).on('click', '[data-role="tologin"]', function() {
            iosOverlay({
                text: '请先登录！',
                duration: 1500
            });
        })
    },
    init: function() {
        this.schedule();
        this.getrelatedPages();
        this.loginStatus();
        this.tologin();
    }
}

detail.init();
